# API_KEY项目

import hashlib
import hmac
import time

import requests

# 调用测试站的接口
# 下单接口
url = "https://test-api.superex.bike/api/free-spot/v3/orders"
# url = "http://localhost:51111/free-spot/v3/orders"

timestamp = str(int(time.time()) * 1000)
# print(timestamp)
# timestamp = '1904038400000'
request_data_to_sign = f"POST&currencyId=2&memoId=2&orderNumber=123456&orderPriceType=2&price=23.2&total=10&tradeType=1&timestamp={timestamp}"

access_secret = "II2Szpnbx3uABfeGQkJx--9EVwjM7DW-CYcNWSZASL4hurpVRvXSUFgqOxaNfPW7"


def generate_signature(data_to_sign, secret):
    """
    签名算法
    :param data_to_sign:
    :param secret:
    :return:
    """
    # 创建 HMAC-SHA256 对象
    sha256_hmac = hmac.new(secret.encode(), data_to_sign.encode(), hashlib.sha256)

    # 生成签名
    return sha256_hmac.hexdigest()


sign_data = generate_signature(request_data_to_sign, access_secret)

headers = {
    "X-Access-Key": "WYlOII_9t8Vm1UU6g-vzeFRO0-xdzoqdD3hNz--ifqvxcQfxTHQ6L6SDoK13guzg",
    "X-Signature": sign_data,
    "X-Timestamp": timestamp,
    "ip": "127.0.0.1",
    "language": "zh-TC"
}

# 下单请求 request_body
order_data = {
    "currencyId": 2,
    "memoId": 2,
    "tradeType": 1,
    "orderPriceType": 2,
    "price": 23.2,
    "orderNumber": 123456,
    "total": 10
}

# 发送JSON格式的数据
response = requests.post(url, json=order_data, headers=headers)

# 打印响应内容
print(response.text)

if __name__ == '__main__':
    # 示例用法
    secret = "II2Szpnbx3uABfeGQkJx--9EVwjM7DW-CYcNWSZASL4hurpVRvXSUFgqOxaNfPW7"
    signature = generate_signature(request_data_to_sign, secret)
    print(signature)
